package com.net.framework.help.utils;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;

import java.util.List;

/**
 * Author：xiaobin.zhang
 * Email：154722566@qq.com
 * Date：2017/1/4 16:52
 * Desc：<--功能描述-->
 */
public class AppAvailableListener {

    public static boolean isWeixinAvilible(Context context) {
        return isClientAvailable(context, "com.tencent.mm");
    }

    public static boolean isQQClientAvailable(Context context) {
        return isClientAvailable(context, "com.tencent.mobileqq");
    }

    public static boolean isWeiboClientAvailable(Context context) {
        return isClientAvailable(context, "com.sina.weibo");
    }

    public static boolean isQZoneClientAvailable(Context context) {
        return isClientAvailable(context, "com.qzone");
    }

    /**
     * 是否安装支付宝
     *
     * @param context
     * @return
     */
    public static boolean isAlipayGphoneClientAvailable(Context context) {
        return isClientAvailable(context, "com.eg.android.AlipayGphone");
    }

    /**
     * 检测是否安装该app
     *
     * @param context
     * @return
     */
    public static boolean isClientAvailable(Context context, String packageName) {
        PackageManager packageManager = context.getPackageManager();
        List pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); ++i) {
                String pn = ((PackageInfo) pinfo.get(i)).packageName;
                if (pn.equals(packageName)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 跳转到应用详情页
     */
    public static void toMarket(Activity context, String packageName) {
        Intent intent = getIntent(packageName);
        boolean b = judge(context, intent);
        if (!b) {
            try {
                context.startActivity(intent);
            } catch (ActivityNotFoundException e) {
                UIUtils.shortToast("手机上未安装应用商店~");
            }
        } else {
            UIUtils.shortToast("手机上未安装应用商店~");
        }
    }

    private static Intent getIntent(String packageName) {
        StringBuilder localStringBuilder = new StringBuilder().append("market://details?id=");
        localStringBuilder.append(packageName);
        Uri localUri = Uri.parse(localStringBuilder.toString());
        return new Intent("android.intent.action.VIEW", localUri);
    }

    /**
     * 判断是否有应用市场
     *
     * @param context      上下文
     * @param marketIntent 跳转意图
     * @return 是否有应用市场true/false
     */
    private static boolean judge(Context context, Intent marketIntent) {
        List<ResolveInfo> localList = context.getPackageManager().queryIntentActivities(marketIntent, PackageManager.GET_RESOLVED_FILTER);
        if ((localList != null) && (localList.size() > 0)) {
            return false;
        } else {
            return true;
        }
    }
}